package com.program.facesigninsystem.controller;

import com.program.facesigninsystem.api.CommonPage;
import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.iteration.entity.SigninclassEntity;
import com.program.facesigninsystem.iteration.entity.UserSigninclassEntity;
import com.program.facesigninsystem.service.SignInClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Api(description = "签到名单模块")
@RestController
@RequestMapping(value = "/list")
public class SignInClassController {

    @Resource
    SignInClassService signInClassService;


    @ApiOperation("excel导入创建签到名单")
    @PostMapping("/excel")
    public ResponseModel excelImport(@ApiParam(value = "excel名单文件",required = true)@RequestParam MultipartFile list,
                                     @ApiParam(value = "名单文件名",required = true)@RequestParam String listName,
                                     @ApiParam(value = "名单创建者ID",required = true)@RequestParam String uid) {

        if (signInClassService.excelImport(list, listName, uid))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "Excel文件内部格式不符合要求！");
    }

    @ApiOperation("获取用户签到名单【分页】")
    @GetMapping("/get")
    public ResponseModel getList(@ApiParam(value = "名单创建者ID",required = true)@RequestParam String uid,
                                 @ApiParam(value = "分页的页数",required = true)@RequestParam int pageNum,
                                 @ApiParam(value = "每页内容的数量",required = true)@RequestParam int pageSize) {

        List<SigninclassEntity> signinclassEntityList = signInClassService.getList(uid, pageNum, pageSize);
        if (signinclassEntityList != null)
            return new ResponseModel(200, "操作成功", CommonPage.restPage(signinclassEntityList));
        else
            return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("获取用户签到名单【不分页】")
    @GetMapping("/get2")
    public ResponseModel getList2(@ApiParam(value = "名单创建者ID",required = true)@RequestParam String uid) {

        List<SigninclassEntity> signinclassEntityList = signInClassService.getList2(uid);
        if (signinclassEntityList != null)
            return new ResponseModel(200, "操作成功", CommonPage.restPage(signinclassEntityList));
        else
            return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("修改用户签到名单名称")
    @PostMapping("/update")
    public ResponseModel update(@ApiParam(value = "签到名单ID",required = true,example = "57")@RequestParam int listId,
                                @ApiParam(value = "修改后的名单名称",required = true)@RequestParam String listName) {

        if (signInClassService.updateSigninClass(listId, listName))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("查看签到名单成员")
    @GetMapping("/getMember")
    public ResponseModel getMember(@ApiParam(value = "签到名单ID",required = true,example = "57")@RequestParam int listId) {

        List<UserSigninclassEntity> userSigninclassEntityList = signInClassService.getMember(listId);
        if (userSigninclassEntityList != null)
            return new ResponseModel(200, "操作成功", userSigninclassEntityList);
        else
            return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("删除签到名单")
    @GetMapping("/del")
    public ResponseModel delList(@ApiParam(value = "签到名单ID",required = true,example = "57") @RequestParam int listId) {

        if (signInClassService.delList(listId))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("批量删除签到名单成员")
    @GetMapping("/delMembers")
    public ResponseModel delMembers(@ApiParam(value = "签到名单ID",required = true,example = "57")@RequestParam int listId, @ApiParam(value = "删除成员的姓名",required = true)@RequestParam String[] names,@ApiParam(value = "疑似弃用，数据库中全为null",required = false)@RequestParam(required = false) String[] signinclass_numbers) {

        if (signInClassService.delMembers(listId, names,signinclass_numbers))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("excel新增签到名单成员")
    @PostMapping("/insertMembers/excel")
    public ResponseModel insertMembersExcel(@ApiParam(value = "签到名单文件",required = true)@RequestParam MultipartFile list, @ApiParam(value = "签到名单ID",required = true)@RequestParam int listId) {

        if (signInClassService.insertMembersExcel(list, listId))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "Excel文件内部格式不符合要求！");
    }


    @ApiOperation("手动新增签到名单成员")
    @PostMapping("/insertMembers/manual")
    public ResponseModel insertMembersManual(@ApiParam(value = "签到名单ID", required = true) @RequestParam int listId,
                                             @ApiParam(value = "被添加成员姓名", required = true) @RequestParam String name,
                                             @ApiParam(value = "成员学号", required = true) @RequestParam String studentId,
                                             @ApiParam(value = "当前名单人数", required = false) @RequestParam(required = false) String signin_number) {

        if (signInClassService.insertMembersManual(listId, name, studentId, signin_number))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "操作失败");
    }
}
